package com.gxa.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.admin.entity.vo.GoodsAuditVo;
import com.gxa.admin.exception.SystemException;
import com.gxa.admin.mapper.GoodsMapper;
import com.gxa.admin.param.GoodsParam;
import com.gxa.admin.service.GoodsService;
import com.gxa.admin.validator.MyValidator;
import com.gxa.o2o.core.dto.ResultDTO;
import com.gxa.o2o.core.pojo.Goods;
import com.gxa.o2o.core.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.nio.cs.ext.MacArabic;

import javax.annotation.Resource;
import javax.validation.Validation;
import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Autowired
    MyValidator validator;

    /**
     * 查询所有商品（不包括已删除的）
     * @return
     */
    @Override
    public ResultDTO listAll() {
        List<Goods> goodsList = goodsMapper.findAll();
        return new ResultDTO(200,"查询成功",goodsList);
    }

    /**
     * 按条件分页查询
     * @param param
     * @return
     */
    @Override
    public ResultDTO listByParam(GoodsParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        List<Goods> goodsList = goodsMapper.findByParam(param);
        PageInfo<Goods> of = PageInfo.of(goodsList);

        return new ResultDTO(200,"查询成功",of);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public ResultDTO findById(String id){
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        return new ResultDTO(200,"查询成功",goods);
    }

    @Override
    public ResultDTO delete(List<String> ids) {

        if(ids == null || ids.size() == 0){
            return new ResultDTO(1001,"传入id错误");
        }
        if (goodsMapper.updateDeletedByIds(ids) != ids.size()) {
            throw new SystemException(1001,"删除失败");
        }
        return new ResultDTO(200,"删除成功");
    }

    @Override
    public ResultDTO offShelf(List<String> ids) {
        if(ids == null || ids.size() == 0){
            return new ResultDTO(1001,"传入id错误");
        }
        if (goodsMapper.updateOffShelfByIds(ids) != ids.size()) {
            throw new SystemException(1001,"下架失败");
        }
        return new ResultDTO(200,"下架成功");
    }

    /**
     * 批量审核通过
     * @param ids
     * @return
     */
    @Override
    public ResultDTO audit(List<String> ids) {
        if(ids == null || ids.size() == 0){
            return new ResultDTO(1001,"传入id错误");
        }
        if (goodsMapper.updateAuditByIds(ids) != ids.size()) {
            throw new SystemException(1001,"审核失败");
        }
        return new ResultDTO(200,"审核成功");
    }

    /**
     * 商品审核
     * @param goodsAuditVo
     * @return
     */
    @Override
    public ResultDTO audit(GoodsAuditVo goodsAuditVo){
        //校验
        validator.validate(goodsAuditVo.getClass());
        //审核
        goodsAuditVo.setGoodsAuditTime(new Timestamp(System.currentTimeMillis()));
        if (goodsMapper.audit(goodsAuditVo) != 1) {
            return new ResultDTO(1001,"审核失败");
        }
        return new ResultDTO(200,"审核成功");
    }

    @Override
    public ResultDTO updateOne(Goods goods) {
        if (goods.getGoodsId() == null || goods.getGoodsId().isEmpty()) {
            return new ResultDTO(1001,"id必传");
        }

        //查询数据库
        Goods dbGoods = goodsMapper.selectByPrimaryKey(goods.getGoodsId());
        //判断id是否存在
        if(dbGoods == null){
            return new ResultDTO(1001,"要修改的商品不存在");
        }
        if(dbGoods.getGoodsIsDeleted() == 1){
            return new ResultDTO(1001,"该商品已删除，不能修改");
        }
        goods.setGoodsUpdateTime(new Timestamp(System.currentTimeMillis()));
        if (goodsMapper.updateByPrimaryKeySelective(goods) != 1) {
            return new ResultDTO(1001,"修改失败");
        }
        return new ResultDTO(200,"修改成功");
    }

    @Override
    public ResultDTO add(Goods goods) {
        //校验
        validator.validate(goods.getClass());
        //生成id
        goods.setGoodsId(UUIDUtil.getUid());
        goods.setGoodsCreateTime(new Timestamp(System.currentTimeMillis()));
        if (goodsMapper.insertSelective(goods) != 1) {
            return new ResultDTO(1001,"添加失败");
        }

        return new ResultDTO(200,"添加成功");
    }
}
